package com.example.hsh.homefragment.utils;


import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Created by HSH on 2016/12/26.
 */

public class ScreenUtil {

    private static ScreenInfo screenInfo;

    private ScreenUtil() {
    }

    private static class SingletonHolder{
        private static final ScreenUtil instance = new ScreenUtil();
    }

    public static  ScreenUtil getInstance(){
        return SingletonHolder.instance;
    }

    public static float getDensity(Context context) {
        if (screenInfo == null) {
            synchronized (ScreenInfo.class) {
                if (screenInfo == null) {
                    screenInfo = new ScreenInfo();
                }
            }
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            screenInfo.density = metrics.density;
            screenInfo.height = metrics.heightPixels;
            screenInfo.width = metrics.widthPixels;
        }

        return screenInfo.density;
    }

    public static float getScreenWidth(Context context) {
        if (screenInfo == null) {
            synchronized (ScreenInfo.class) {
                if (screenInfo == null) {
                    screenInfo = new ScreenInfo();
                }
            }
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            screenInfo.density = metrics.density;
            screenInfo.height = metrics.heightPixels;
            screenInfo.width = metrics.widthPixels;
        }

        return screenInfo.width;
    }

    public static float getScreenHeight(Context context) {
        if (screenInfo == null) {
            synchronized (ScreenInfo.class) {
                if (screenInfo == null) {
                    screenInfo = new ScreenInfo();
                }
            }
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            screenInfo.density = metrics.density;
            screenInfo.height = metrics.heightPixels;
            screenInfo.width = metrics.widthPixels;
        }

        return screenInfo.height;
    }

    static class ScreenInfo{
        float density;
        float width;
        float height;
    }
}
